package zhoufu.sz.sanxiao_check.ui.adapter.items;

import java.util.ArrayList;
import java.util.List;

import eu.davidea.flexibleadapter.items.IExpandable;
import eu.davidea.flexibleadapter.items.IFlexible;
import eu.davidea.flexibleadapter.items.IHeader;

/**
 * Created by hyc on 16-8-3.
 * 项目 ： SanXiao_Check
 * 开发 ： hyc
 * 时间 ： 16-8-3 下午4:31
 * 功能 ： TODO
 */

public abstract class ExpandableHeaderItem<E,S extends IFlexible> extends DataBindingItem<E>
        implements IExpandable<DataBindingItem.ViewHolder, S>,
        IHeader<DataBindingItem.ViewHolder> {

    private boolean mExpanded = false;

    public ExpandableHeaderItem(){
        setDraggable(false);
        //We start with header shown and expanded
        setHidden(false);
        setExpanded(true);
        //NOT selectable (otherwise ActionMode will be activated on long click)!
        setSelectable(false);
    }
    /* subItems list */
    private List<S> mSubItems;
    @Override
    public boolean isExpanded() {
        return mExpanded;
    }

    @Override
    public void setExpanded(boolean expanded) {
        this.mExpanded=expanded;
    }

    @Override
    public int getExpansionLevel() {
        return 0;
    }

    @Override
    public List<S> getSubItems() {
        return mSubItems;
    }

    public final boolean hasSubItems() {
        return mSubItems!= null && mSubItems.size() > 0;
    }

    public boolean removeSubItem(S item) {
        return item != null && mSubItems.remove(item);
    }

    public boolean removeSubItem(int position) {
        if (mSubItems != null && position >= 0 && position < mSubItems.size()) {
            mSubItems.remove(position);
            return true;
        }
        return false;
    }

    public void addSubItem(S subItem) {
        if (mSubItems == null)
            mSubItems = new ArrayList<>();
        mSubItems.add(subItem);
    }

    public void addSubItem(int position, S subItem) {
        if (mSubItems != null && position >= 0 && position < mSubItems.size()) {
            mSubItems.add(position, subItem);
        } else
            addSubItem(subItem);
    }

}
